<template>
  <div class="hi-card bg_card mt-8">
    <div class="left">
      <div class="left-txt c_90">{{ leftTxt }}</div>
      <div class="left-msg c_007DFF" v-if="showMsg">
        {{ leftMsg }}
      </div>
    </div>
    <div class="right">
      <div class="hi-slider">
        <div class="bar bg_slider" ref="bar" @click.stop="clickBar($event)">
          <div
            class="progress"
            :style="{ width: switchLeft, backgroundColor: progressColor }"
          ></div>
          <div
            class="block"
            ref="block"
            :style="{ left: switchLeft }"
            @touchstart.stop="touchStart($event)"
            @touchmove.stop="touchMove($event)"
            @touchend.stop="touchEnd($event)"
          ></div>
        </div>
      </div>
      <div v-if="isShowRange" class="range-value c_60">
        <div
          v-for="(item, i) in rangeList"
          :key="i"
          :style="{ left: i * ((barWidth - 24) / rangeLength) + 'px' }"
          class="range-item"
        >
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "sliderCard",
  props: {
    leftTxt: {
      type: String,
      default: "",
    },
    // 当前刻度值
    percent: {
      type: [String, Number],
      default: "",
    },
    // 单位
    unit: {
      type: String,
      default: "",
    },
    // 步长
    step: {
      type: [String, Number],
      deafult: 1,
    },
    // 最大值
    maxValue: {
      type: [String, Number],
      deafult: 100,
    },
    // 是否展示刻度值
    isShowRange: {
      type: Boolean,
      deafult: false,
    },
    // 刻度数组
    rangeList: {
      type: Array,
      deafult: [],
    },
    // 进度条颜色
    progressColor: {
      type: String,
      deafult: "#0a59f7", // 华为规范蓝色
    },
  },
  data() {
    return {
      // 滑块宽度
      blockWidth: 24,
      // 滑杆长度
      barWidth: 0,
      // 可滑动最大长度
      max: 0,
      // 滑块滑动前左偏移量
      initBlock: 0,
      // 新位置
      newPosition: 0,
      // 蓝色进度条长度
      switchLeft: 0,
      // 最初触屏点X坐标
      startX: 0,
      // 是否显示实时数据
      showMsg: true,
      // 实时显示数据
      showVal: 0,
      // 计算后的步数
      steps: 1,
    };
  },
  computed: {
    // 实时数据+单位处理
    leftMsg() {
      return `${this.showVal}${this.unit}`;
    },
    rangeLength() {
      return this.maxValue / this.step;
    },
  },
  created() {},
  mounted() {
    this.initData();
    console.log(this.rangeLength);
    console.log(this.showVal);
  },
  methods: {
    // 初始化slider数据
    initData() {
      this.blockWidth = this.$refs.block.offsetWidth;
      this.barWidth = this.$refs.bar.offsetWidth;
      this.max = this.barWidth - this.blockWidth;
      this.initBlock = this.$refs.block.offsetLeft;
    },
    // 开始触屏
    touchStart(e) {
      this.initData();
      // 触屏初始点X坐标
      this.startX = e.targetTouches[0].pageX;
    },
    // 滑屏
    touchMove(e) {
      // 滑动的长度数值
      let diff = e.targetTouches[0].pageX - this.startX;
      console.log("touchMove");
      // 滑动一次后的位置
      this.newPosition = this.initBlock + diff;
      this.setPosition(this.newPosition);
      this.showVal = parseInt(this.steps * this.step);
    },
    // 触屏结束
    touchEnd() {
      // 滑动结束后滑块的位置
      this.initBlock = this.newPosition;
      this.setPosition(this.newPosition);
      this.showVal = parseInt(this.steps * this.step);
      this.$emit("handleTouchEnd", this.showVal);
      // console.log('触屏结束');
    },
    // 滑块移动位置 随步长做调整
    setPosition(newPosition) {
      console.log("newPosition", newPosition);
      if (newPosition === null || isNaN(newPosition)) return;
      if (newPosition < 0) {
        newPosition = 0;
      } else if (newPosition > this.max) {
        newPosition = this.max;
      }
      // 滑块一个单位代表长度
      const lengthPerStep = (this.max * this.step) / this.maxValue;
      // 滑动的步数（单位）
      this.steps = Math.round(newPosition / lengthPerStep);
      this.switchLeft = lengthPerStep * this.steps + "px";
    },
    clickBar(e) {
      console.log("clickBar");
      let left = e.offsetX > this.max ? this.max : e.offsetX;
      this.switchLeft = left + "px";
      this.setPosition(left);
      this.showVal = parseInt(this.steps * this.step);
      this.$emit("handleTouchEnd", this.showVal);
    },
  },
  watch: {
    percent: {
      handler: function (val) {
        this.showVal = val;
        this.$nextTick(() => {
          // 接收传入的数值
          const GET_VALUE = (val / this.maxValue) * this.max;
          this.switchLeft = GET_VALUE + "px";
        });
      },
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped>
.hi-card {
  border-radius: 16px;
  background-color: var(--box_bg);
  box-shadow: 0 3px 6px 0 var(--shadow_cell_card);
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 264px;
  .left {
    margin: auto 0;
    line-height: 1.33;
    padding-left: 24px;
    .left-txt {
      color: var(--font);
      width: 65.6px;
      font-size: 16px;
    }
    .left-msg {
      color: var(--blue);
      font-size: 12px;
      margin-top: 2px;
    }
  }
  .right {
    flex: 1;
    width: 100%;
    margin: 0 24px 0 8px;
    line-height: 1.33;
    .hi-slider {
      height: 24px;
      width: 100%;
      position: relative;
      transform: rotate(-90deg);
      .bar {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 100%;
        height: 2px;
        border-radius: 1px;
        background-color: var(--switch_bg);
        .progress {
          // background-color: var(--color_blue);
          height: 100%;
          width: 100%;
          border-radius: 1px;
        }
        .block {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          height: 24px;
          width: 24px;
          border-radius: 50%;
          transition: all 0.2s;
          border: 0.25px solid var(--border);
          background-color: #ffffff;
          box-shadow: 0 3px 8px 0 rgba(0, 0, 0, 0.15),
            0 1px 1px 0 rgba(0, 0, 0, 0.16);
        }
      }
    }
    .range-value {
      display: flex;
      font-size: 10px;
      position: relative;
      width: 100%;
      height: 16px;
      .range-item {
        position: absolute;
        top: 0;
      }
    }
  }
}
.mt-8 {
  margin-top: 8px;
}
</style>
